package lc.oa.net.ws;

import android.annotation.SuppressLint;
import android.util.Log;

import org.jetbrains.annotations.Nullable;

import lc.oa.BuildConfig;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.WebSocket;
import okhttp3.WebSocketListener;
import okio.ByteString;

/**
 * @Description: Web Socket处理
 * @Author:  lucifer
 * @CreateDate: 2020/8/7 16:33
 * @UpdateUser:
 * @UpdateDate:
 * @UpdateRemark:
 * @Version: 1.0
 */
public class WebSocketHandler extends WebSocketListener {
    private static WebSocketHandler instance;

    private final static String wsServiceURL= BuildConfig.WS_API_URL;
    private OkHttpClient client = new OkHttpClient.Builder()
            .build();
    private WebSocket webSocket;
    private int status;
    private OAWebSocketListener listener;
   // private String token;
    private final String fromApp="mobile-oa-v1";

    private WebSocketHandler(){}

     /**
      * @method  获取实例
      * @description 描述一下方法的作用
      * @date: 2020/8/7 17:25
      * @author: lucifer
      * @param
      * @return
      */
    public static synchronized WebSocketHandler getInstance(){
        if (null==instance){
            instance=new WebSocketHandler();
        }
        return instance;
    }
     /**
      * @method  获取 实例
      * @description 描述一下方法的作用
      * @date: 2020/8/7 17:24
      * @author: lucifer
      * @param _listener 回调处理事件
      * @return
      */
    public static synchronized WebSocketHandler getInstance(OAWebSocketListener _listener){
        if (_listener==null) return getInstance();
        if (null==instance){
            instance=new WebSocketHandler();
        }
        instance.listener=_listener;
        return instance;
    }
 /**
  * @method  连接 web socket
  * @description 描述一下方法的作用
  * @date: 2020/8/7 18:04
  * @author: lucifer
  * @param
  * @return
  */
    public void connect(String token){
        String userId="dfb041b90db24953b605b6c7e6f42c35";
        String wsServiceFullURL=wsServiceURL+"/"+userId+"/"+fromApp+"?token="+token;
        Request request=new Request.Builder().url(wsServiceFullURL).build();
        webSocket=client.newWebSocket(request,this);
        status=1;
    }

    @SuppressLint("LongLogTag")
    @Override
    public void onOpen(WebSocket webSocket, Response response) {
        super.onOpen(webSocket, response);
        if (listener!=null){
            listener.onOpen(webSocket,response);
        }
        Log.i("-------------socket service---------------------------","onOpen");

    }

    @SuppressLint("LongLogTag")
    @Override
    public void onClosed(WebSocket webSocket, int code, String reason) {
        super.onClosed(webSocket, code, reason);
        Log.i("-------------socket service---------------------------","onClosed");

    }

    @SuppressLint("LongLogTag")
    @Override
    public void onClosing(WebSocket webSocket, int code, String reason) {
        super.onClosing(webSocket, code, reason);
        Log.i("-------------socket service---------------------------","onClosing");

    }

    @SuppressLint("LongLogTag")
    @Override
    public void onMessage(WebSocket webSocket, String text) {
        super.onMessage(webSocket, text);
        Log.i("-------------socket service---------------------------","onMessage");
        if (listener!=null){
            listener.onMessage(webSocket,text);
        }
    }

    @SuppressLint("LongLogTag")
    @Override
    public void onFailure(WebSocket webSocket, Throwable t, @Nullable Response response) {
        super.onFailure(webSocket, t, response);
        Log.i("-------------socket service---------------------------","onFailure",t);
    }

    @SuppressLint("LongLogTag")
    @Override
    public void onMessage(WebSocket webSocket, ByteString bytes) {
        super.onMessage(webSocket, bytes);
        Log.i("-------------socket service---------------------------","onMessage");
        if (listener!=null){
            listener.onMessage(webSocket,bytes);
        }
    }
}
